﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;

public partial class RentalOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        MembershipUser user = Membership.GetUser();

        if ((object)user != null)
        {

            DVDTableAdapters.DataTable6TableAdapter ta1 = new DVDTableAdapters.DataTable6TableAdapter();
            DataTable dt = new DataTable();
            dt = ta1.GetData((Guid)user.ProviderUserKey);
            GridView1.DataSource = dt;

            GridView1.DataBind();
        }
        else 
        {
            Response.Redirect("~/404.aspx");
        }


    }


    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        MembershipUser user = Membership.GetUser();

        if ((object)user != null)
        {
            TableCell MovieID = GridView1.Rows[e.RowIndex].Cells[1];
            

            //Delete from user RentalList
            DVDTableAdapters.RentalListMoviesTableAdapter ta = new DVDTableAdapters.RentalListMoviesTableAdapter();
            ta.DeleteMovieRentalList(Convert.ToInt32(MovieID.Text), (Guid)user.ProviderUserKey);
        }
        else
        {
            Response.Redirect("~/404.aspx");
        }

        user = null;
        Response.Redirect(Request.RawUrl);
    }

    protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
    {   
        DVDTableAdapters.RentalListMoviesTableAdapter ta = new DVDTableAdapters.RentalListMoviesTableAdapter();
        MembershipUser user = Membership.GetUser();

        if (user != null)
        {

            if (e.CommandName.CompareTo("IncreasePriority") == 0)
            {

                int rowIndex = Convert.ToInt32(e.CommandArgument);
                TableCell MovieIDCell = GridView1.Rows[rowIndex].Cells[1];
                TableCell PriorityCell = GridView1.Rows[rowIndex].Cells[6];

                int MovieID = Convert.ToInt32(MovieIDCell.Text);
                int Priority1 = Convert.ToInt16(PriorityCell.Text);
                int NewPriority1 = Priority1 + 1;
                short Priority = (short)Priority1;
                short NewPriority = (short)NewPriority1;

                short HighestPriority = (short)ta.GetHighestPriority((Guid)user.ProviderUserKey);

                if (Priority < HighestPriority)
                {

                    ta.UpdatePriorityBaseUserkey(Priority, NewPriority, (Guid)user.ProviderUserKey);
                    ta.UpdatePriorityBaseMovieID(NewPriority, MovieID, (Guid)user.ProviderUserKey);

                }
            }
            else if (e.CommandName.Equals("DecreasePriority"))
            {

                int rowIndex = Convert.ToInt32(e.CommandArgument);
                TableCell MovieIDCell = GridView1.Rows[rowIndex].Cells[1];
                TableCell PriorityCell = GridView1.Rows[rowIndex].Cells[6];

                int MovieID = Convert.ToInt32(MovieIDCell.Text);
                int Priority1 = Convert.ToInt16(PriorityCell.Text);
                int NewPriority1 = Priority1 - 1;
                short Priority = (short)Priority1;
                short NewPriority = (short)NewPriority1;

                short HighestPriority = (short)ta.GetHighestPriority((Guid)user.ProviderUserKey);

                if (Priority > 1)
                {

                    ta.UpdatePriorityBaseUserkey(Priority, NewPriority, (Guid)user.ProviderUserKey);
                    ta.UpdatePriorityBaseMovieID(NewPriority, MovieID, (Guid)user.ProviderUserKey);

                }

            }
            else if (e.CommandName.Equals("delete"))
            {
                TableCell MovieID = GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1];

                //Change Priorities
                DVDTableAdapters.RentalListMoviesTableAdapter ta1 = new DVDTableAdapters.RentalListMoviesTableAdapter();
                short prioDelete = (short)ta1.CheckMovieOnList(Convert.ToInt32(MovieID.Text), (Guid)user.ProviderUserKey);

                DVDTableAdapters.RentalListMovies1TableAdapter ta2 = new DVDTableAdapters.RentalListMovies1TableAdapter();
                DataTable dt = ta2.GetData((Guid)user.ProviderUserKey);

                foreach (DataRow row in dt.Rows)
                {
                    if (Convert.ToInt32(row["Priority"]) > prioDelete)
                    {
                        int newPrio = Convert.ToInt32(row["Priority"]);
                        newPrio -= 1;
                        ta2.UpdateQuery((short)newPrio, Convert.ToInt32(row["RentalListMoviesID"]), (Guid)user.ProviderUserKey);
                    }
                }

                //DElete from rental list
                DVDTableAdapters.RentalListMoviesTableAdapter ta3 = new DVDTableAdapters.RentalListMoviesTableAdapter();
                ta.DeleteMovieRentalList(Convert.ToInt32(MovieID.Text), (Guid)user.ProviderUserKey);
            }
            user = null;
            Response.Redirect(Request.RawUrl);
        }
        else
        {
            Response.Redirect("~/404.aspx");
        }


    }


}
